#include "SGMDataCollector.h"

SGMDataCollector::SGMDataCollector(void)
{
}

SGMDataCollector::~SGMDataCollector(void)
{
	meshes.clear();
}

void SGMDataCollector::AddMesh(Scene3DMesh *mesh)
{
	meshes.insert(meshes.end(), mesh);
}

void SGMDataCollector::SaveToFile(const char *file_name)
{
	std::ofstream file(file_name, std::ios::binary);
	SaveMeshes(file);
	file.close();
}

void SGMDataCollector::SaveMeshes(std::ostream &os)
{
	int meshes_count = meshes.size();

	StreamWriter::WriteInt(os, meshes_count);

	for (int i_meshes = 0; i_meshes < meshes_count; i_meshes++)
	{
		Scene3DMesh *mesh = meshes[i_meshes];

		StreamWriter::WriteInt(os, mesh ->id);
		StreamWriter::WriteInt(os, mesh ->materialIndex);

		//amount of vetices
		StreamWriter::WriteInt(os, mesh ->faces_count * 3);

		for (int i = 0; i < mesh ->faces_count; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				StreamWriter::WriteFloat(os, mesh ->verticles[mesh ->faces[i].vert_i[j]].x);
				StreamWriter::WriteFloat(os, mesh ->verticles[mesh ->faces[i].vert_i[j]].y);
				StreamWriter::WriteFloat(os, mesh ->verticles[mesh ->faces[i].vert_i[j]].z);

				StreamWriter::WriteFloat(os, mesh ->normals[mesh ->faces[i].norm_i[j]].x);
				StreamWriter::WriteFloat(os, mesh ->normals[mesh ->faces[i].norm_i[j]].y);
				StreamWriter::WriteFloat(os, mesh ->normals[mesh ->faces[i].norm_i[j]].z);

				StreamWriter::WriteFloat(os, mesh ->tex_coords[mesh ->faces[i].coord_i[j]].u);
				StreamWriter::WriteFloat(os, mesh ->tex_coords[mesh ->faces[i].coord_i[j]].v);
			}
		}
	}
}
